package org.unique.generator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ConfigUtils {
	  public static Map<String, String> propMap = null;  
      
	    /** 初始化 */  
	    public static void init(String path){  
	        propMap = new HashMap<String, String>();  
	        BufferedReader br = null;  
	        try {  
	            br = new BufferedReader(new FileReader(path));  
	            String line = "";  
	            while((line = br.readLine()) != null){  
	                if(line.indexOf("=") != -1 && !line.trim().startsWith("#")  
	                         && !line.trim().startsWith("//")){  
	                    String[] lineArr = line.split("=");  
	                    propMap.put(lineArr[0].trim(), lineArr[1].trim());  
	                }  
	            }  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        } finally {  
	            try {  
	                br.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	    }  
	    
	    /** 
	     * 获取所有键值对组成的Map 
	     * @param path 配置文件所在路径 
	     * @return 
	     */  
	    public static Map<String, String> getPropMap(String path){  
//	      if(propMap == null || propMap.isEmpty()){  
	        init(path);  
//	      }  
	          
	        return propMap;  
	    }  
	      
	    /** 
	     * 获取属性值 
	     * @param path 配置文件所在路径 
	     * @param key 属性名 
	     * @return 
	     */  
	    public static String getPropValue(String path, String key){  
	        init(path);  
	        return propMap.get(key);  
	    }  
	      
	    /** 
	     * 设置某个属性的值 
	     * @param path 
	     * @param key 
	     * @param value 
	     */  
	    public static void setPropValue(String path, String key, String value){  
	        Map<String, String> map = new HashMap<String, String>();  
	        map.put(key, value);  
	        setPropValues(path, map);  
	    }  
	      
	    /** 
	     * 根据Map设置属性 
	     * @param path 
	     * @param map 
	     */  
	    public static void setPropValues(String path, Map<String, String> map){  
	        BufferedReader br = null;  
	        BufferedWriter bw = null;  
	        try {  
	            br = new BufferedReader(new FileReader(path));  
	              
	            StringBuffer sb = new StringBuffer();  
	            String line = "";  
	            // 读取文件并对替换文件内容  
	            while((line = br.readLine()) != null){  
	                if(line.indexOf("=") != -1 && !line.trim().startsWith("#")  
	                         && !line.trim().startsWith("//")){  
	                    String[] lineArr = line.split("=");  
	                    String key = lineArr[0].trim();  
	                    String newValue = map.get(key);  
	                    if(null != newValue && !"".equals(newValue)){  
	                        sb.append(key).append(" = ").append(newValue).append("\r\n");  
	                    }else{  
	                        sb.append(line).append("\r\n");  
	                    }  
	                }else{  
	                    sb.append(line).append("\r\n");  
	                }  
	            }  
	            br.close();  
	            bw = new BufferedWriter(new FileWriter(path));  
	            // 写入文件  
	            bw.write(sb.toString());  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        } finally {  
	            try {  
	                br.close();  
	                bw.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	    }  
	      
}
